package com.java.test.system.gametfeemanage;

import com.java.test.system.gametfeemanage.dto.GameFeeAddDTO;
import com.java.test.system.gametfeemanage.dto.GameFeeQueryDTO;
import com.java.test.system.gametfeemanage.dto.GameFeeUpdateDTO;
import com.java.test.system.gametfeemanage.entity.GameFeeEntity;
import com.java.test.system.gametfeemanage.vo.GameFeeVO;
import com.java.test.system.gametypemanage.entity.GameTypeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 游戏抽水DAO
 * @author lijie1
 * @date 2020/6/29
 */
@Component
public class GameFeeManageDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询游戏抽水列表
     * @author lijie1
     * @date 2020/6/29
     */
    public List<GameFeeEntity> selectGameFeeList(GameFeeQueryDTO queryDTO) {
        String sql = "SELECT t.*,t1.gname gameTypeStr,t.fee_scale*100 feeScaleStr  " +
                "FROM sys_game_fee t LEFT JOIN sys_game_type t1 on t.game_type = t1.id";

        List<GameFeeEntity> list = jdbcTemplate.query(sql, new GameFeeEntity());

        return list;
    }
    /**
     * 获取游戏抽水BY游戏名称
     * @param name
     * @return
     */
    public GameFeeEntity getByGName(String name) {
        String sql = "select * from sys_game_fee where name =?";
        GameFeeEntity info = null;
        try {
            info= jdbcTemplate.queryForObject(sql, new GameFeeEntity(), name);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 获取游戏抽水BYid
     * @param id
     * @return
     */
    public GameFeeEntity getById(String id) {
        String sql = "select *,'' gameTypeStr,'' feeScaleStr  from sys_game_fee where id = ?";
        GameFeeEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new GameFeeEntity(), id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

        return info;
    }

    /**
     * 添加游戏抽水
     * @author lijie1
     * @date 2020/6/29 0029
     */
    public void insertGameFee(GameFeeAddDTO addDTO) {
        String sql ="insert into sys_game_fee(name,game_type,fee_scale,remarks,operation,createTime,status) " +
                "values (?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,addDTO.getName(),addDTO.getGameType(),addDTO.getFeeScale(),addDTO.getRemarks(),addDTO.getOperation(),addDTO.getCreateTime(),addDTO.getStatus());
    }
    /**
     * 判断除开当前id是否有相同名称
     * @author lijie1
     * @date 2020/6/29 0029
     */
    public GameFeeEntity getByKeyExcludeId(String name,String gameType, String id) {
        String sql = "select * from sys_game_fee where name = ? and game_type = ? and id <> ?";
        GameFeeEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new GameFeeEntity(),name,gameType,id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 修改游戏抽水
     * @author lijie1
     * @date 2020/6/29 0029
     */
    public void updateById(GameFeeUpdateDTO updateDTO) {
        String sql = "update sys_game_fee " +
                "set  name = ?,game_type = ?,fee_scale = ?,remarks = ?,modifyTime=?,status=? " +
                "where id = ?";

        jdbcTemplate.update(sql, updateDTO.getName(),updateDTO.getGameType(),updateDTO.getFeeScale(),
                updateDTO.getRemarks(),updateDTO.getModifyTime(),updateDTO.getStatus(),
                updateDTO.getId());
    }

    /**
     * 删除游戏抽水
     * @author lijie1
     * @date 2020/6/29 0029
     */
    public void deleteById(String id) {
        String sql ="DELETE FROM sys_game_fee WHERE id = ?";
        jdbcTemplate.update(sql,id);
    }
}
